package com.example.mode;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.mode.activity.CountdownActivity;
import com.example.mode.activity.LoginActivity;
import com.example.mode.activity.PodcastActivity;
import com.example.mode.activity.ReadingActivity;
import com.example.mode.activity.RegisterActivity;
import com.example.mode.activity.SearchWordActivity;
import com.example.mode.activity.SportActivity;
import com.example.mode.activity.ToDoListActivity;
import com.example.mode.activity.WeatherActivity;
import com.example.mode.adapter.ViewPagerAdapter;
import com.example.mode.fragment.FocusFragment;
import com.example.mode.fragment.MusicFragment;
import com.example.mode.fragment.NearbyFragment;
import com.example.mode.fragment.NewsFragment;
import com.example.mode.fragment.RecommendFragment;
import com.example.mode.fragment.VideosFragment;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private ImageView menu;
    private TabLayout tabLayout;
    private ViewPager2 viewPager2;
    private ViewPagerAdapter viewPagerAdapter;
    String[] titleArr = new String[]{"情绪", "夸夸", "新闻", "视频", "音乐", "单词"};
    private NavigationView navigationView;
    private TextView login, register;
    private ImageView weather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.drawer_layout);
        menu = findViewById(R.id.menuDrawer);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager2 = findViewById(R.id.viewPager2);
        navigationView = findViewById(R.id.nav_view);
        View headerLayout = navigationView.getHeaderView(0);
        weather = headerLayout.findViewById(R.id.weather);
        login = headerLayout.findViewById(R.id.login);
        register = headerLayout.findViewById(R.id.register);
        menu.setOnClickListener(view -> drawerLayout.open());

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new FocusFragment());
        fragmentList.add(new RecommendFragment());
        fragmentList.add(new NewsFragment());
        fragmentList.add(new VideosFragment());
        fragmentList.add(new MusicFragment());
        fragmentList.add(new NearbyFragment());
        viewPagerAdapter = new ViewPagerAdapter(this, fragmentList, titleArr);
        viewPager2.setAdapter(viewPagerAdapter);

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewPager2, true,
                ((tab, position) -> {
                    tab.setText(viewPagerAdapter.getTitle(position));
                }));
        tabLayoutMediator.attach();

        // 登录注册页跳转
        weather.setOnClickListener(view -> {
            Intent intentWeather = new Intent(this, WeatherActivity.class);
            startActivity(intentWeather);
        });
        login.setOnClickListener(view -> {
            Intent intentLogin = new Intent(this, LoginActivity.class);
            startActivity(intentLogin);
        });
        register.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, RegisterActivity.class);
            startActivity(intentRegister);
        });

        // 设置导航项的点击监听器
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();
                if (id == R.id.nav_gallery) {
                    Intent intent = new Intent(MainActivity.this, ToDoListActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_search_word) {
                    Intent intent = new Intent(MainActivity.this, SearchWordActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_reading) {
                    Intent intent = new Intent(MainActivity.this, ReadingActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_podcast) {
                    Intent intent = new Intent(MainActivity.this, PodcastActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_calendar) {
                    //Intent intent = new Intent(MainActivity.this, CalendarActivity.class);
                    //startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_countdown) {
                    Intent intent = new Intent(MainActivity.this, CountdownActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_sport) {
                    Intent intent = new Intent(MainActivity.this, SportActivity.class);
                    startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_service) {
                    // Intent intent = new Intent(MainActivity.this, ServiceActivity.class);
                    // startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                } else if (id == R.id.nav_slideshow) {
                    // Intent intent = new Intent(MainActivity.this, MoreActivity.class);
                    // startActivity(intent);
                    drawerLayout.closeDrawers();
                    return true;
                }
                return false;
            }
        });
    }
}